<?php
    class WZ_Url
    {
        public static function getUrl($routeName, $params = array(), $urlSite = true, $extension = '')
        {
            $routes = \WZ::get('confRoutes')->confRoutes;
            $urlSite = (true === $urlSite) ? urlsite(false) : '';
            if (isset($routes[$routeName])) {
                $route = $route2 = $routes[$routeName]['route'];
                $conf = $routes[$routeName]['defaults'];
                
                if (!count($params)) {
                    return $urlSite . $route;
                }
                
                $r = str_replace(':any', '', $route2);
                $r = str_replace(':num', '', $r);
                $r = str_replace(':alnum', '', $r);
                $r = str_replace(':segment', '', $r);
                $r = str_replace(':alpha', '', $r);
                $tabUri = explode('[]', $r);
                $i = 1;
                foreach ($tabUri as $elem) {
                    $route2 = str_replace($elem, '%%', $route2);
                    $i++;
                }
                $route2 = substr($route2, 2, -2);
                $tabUri = explode('%%', $route2);
                $route = \WZ::str_replace_first($tabUri[0], $params[$conf['param1']], $route);

                if (count($tabUri) > 1) {
                    for ($i = 1 ; $i < count($tabUri) ; $i++) {
                        if (isset($conf['param' . ($i + 1)])) {
                            $var = \WZ_Route::cleanVar($tabUri[$i]);
                            $route = \WZ::str_replace_first($var, $params[$conf['param' . ($i + 1)]], $route);
                        }
                    }
                }
                return $urlSite . $route;
            }
            return $urlSite;
        }
        
        public static function getHome()
        {
            return \WZ::get('urlsite');
        }
    }